package com.example.demo.controller;

import com.example.demo.entity.Instrument;
import com.example.demo.entity.Notation;

import com.example.demo.service.NotationService;
import com.example.demo.util.FileUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("v1")
public class NotationController {

    @Autowired
    private NotationService notationService;


    //分页查询所有乐谱信息
    @GetMapping("/pageInfo")
    public PageInfo<Notation> getPagesNotation(Integer pageNum, Integer pageSize){
        PageInfo<Notation> pageInfo = notationService.findNotationPage(pageNum,pageSize);
        return pageInfo;
    }



    //查询所有乐谱信息
    @GetMapping("/notation1")
    public List<Notation> getAllNotation(){
        return notationService.findNotationAll();
    }




    //id查询乐谱信息
    @GetMapping("/notation2/{notation_id}")
    public Notation getNotationById(@PathVariable Integer notation_id) {
        return notationService.findNotationById(notation_id);
    }


    //添加乐谱信息
    @PostMapping("/notation3")
    public Integer addNotationInfo(@RequestBody Notation notation) {
        System.out.println(notation);
        return notationService.insertNotation(notation);
    }

    //修改乐谱信息
    @PutMapping("/notation4")
    public Integer updateNotationInfo(@RequestBody Notation notation) {
        System.out.println(notation);
        return notationService.updateNotation(notation);
    }

    //删除乐谱信息
    @DeleteMapping("/notation5/{notation_id}")
    public Integer deleteNotationInfo(@PathVariable Integer notation_id) {
        return notationService.deleteNotation(notation_id);
    }

    //上传图片
    @PostMapping("/notation/upload")
    public String getPicFileName(MultipartFile file){
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf('.'));

        String filePath = FileUtil.getUploadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis()+typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return newFileName;
    }

//    //关联查询
//    @GetMapping("/notation6")
//    public List<ViewNotationImageInfo> getAllNotationInfo(){
//        return notationService.findNotationInfo();
//    }

    @GetMapping("/notation/hot")
    public List<Notation> getHotNotations() {
        return notationService.getHotNotations();
    }


}